package com.learn.udp;

import org.junit.Test;

import java.io.IOException;
import java.net.*;

/**
 * UDP网络编程
 */
public class UDPTest {
    @Test
    public void sender() throws IOException {
        DatagramSocket socket = new DatagramSocket();

        //数据包中相关内容
        String str= "I'm Sender";
        byte[] data = str.getBytes();
        InetAddress inet = InetAddress.getLocalHost();

        //封装数据包
        DatagramPacket packet = new DatagramPacket(data, 0, data.length, inet, 9090);
        socket.send(packet);

        socket.close();
    }

    @Test
    public void receiver() throws IOException{
        DatagramSocket socket = new DatagramSocket(9090);

        byte[] buffer = new byte[100];
        DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);

        socket.receive(packet);

        System.out.println(new String(packet.getData(), 0, packet.getLength()));
    }
}
